import 'dart:async';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:shopping_group/base/page_constance.dart';
import 'package:shopping_group/baseblocs/bloc_provider.dart';
import 'package:shopping_group/http/apis/apis.dart';
import 'package:shopping_group/http/http_request_all.dart';
import 'package:shopping_group/modle/person/entity/money_order_entity.dart';
import 'package:shopping_group/modle/person/ui/change_money_view_page.dart';
import 'package:shopping_group/utils/network_utils.dart';
import 'package:shopping_group/utils/route_util.dart';

class SelectMoneyToYjBloc implements BlocBase {
  List<MoneyOrderEntity> _listEntity = [];

  StreamController<Object> _initController = StreamController<Object>();
  StreamSink get initDate => _initController.sink;

  StreamController<Object> _allController = StreamController<Object>();
  StreamSink get _addDate => _allController.sink;
  Stream<Object> get sendData => _allController.stream;

  StreamController<Object> _changeController = StreamController<Object>();
  StreamSink get changeDate => _changeController.sink;

  SelectMoneyToYjBloc() {
    _initController.stream.listen(initData);
    _changeController.stream.listen(changData);
  }

  void changData(data) {
    if (data is Map) {
      _changeTask(data);
    }
  }

  void initData(data) {
    if (data is Map) {
      _initTask(data);
    }
  }

  void _changeTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext)
        .requestFocus(FocusNode());
    BotToast.showLoading();
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.MONEY_YONGJIN_URL, data: data, headers: {},
            success: (result) {
          BotToast.closeAllLoading();
          if (null != result) {
            BotToast.showText(text: '转换成功');
            RouteUtil.popUntil(PageConstance.MAIN_PAGE);
            RouteUtil.goParameter(ChangeViewMoneyPage());
          }
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  void _initTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext)
        .requestFocus(FocusNode());
    BotToast.showLoading();
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.MONEY_LIST_URL, data: data, headers: {},
            success: (result) {
          BotToast.closeAllLoading();
          if (null != result) {
            List list = List.from(result);
            for (var item in list) {
              _listEntity.add(MoneyOrderEntity.fromJson(item));
            }
          }
          _addDate.add(_listEntity);
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  @override
  void dispose() {
    _initController?.close();
    _allController?.close();
    _changeController?.close();
  }
}
